<?php

/*
*附件上传接口类
*/
namespace interface\v1;

class fileUploadAppClass extends index
{
    /**
     * 上传附件
     */
    public function uploadFile()
    {
//        exit(print_r($_FILES));
        $file = $_FILES['uploadFile'];
        $power = $this->post('power');
        $type = $this->post('fileType');
        if (!$this->power($power, 'edit')) {
            $this->resp['warn'] = '你还没有编辑权限';
        } elseif (empty($type)) {
            $this->resp['warn'] = '未找到上传的类型';
        } else {
            $result = $this->saveFile($file, $power, explode(',', para('fileFormat')));
            if (!$result['code']) {
                $this->resp['warn'] = $result['warn'];
            } else {
                $field = array('id' => suiji(), 'stid' => $this->control['stid'], 'target' => $type, 'name' => $result['fileName'], 'format' => $result['suffix'], 'size' => $result['size'], 'src' => $result['src'], 'updateTime' => $this->time, 'time' => $this->time);
                $warn = insert(array('table' => 'file', 'field' => $field));
                if ($warn == '新增成功') {
                    $this->code = 1;
                    $data['result']['id'] = $field['id'];
                    $data['result']['src'] = root . $field['src'];
                    $this->resp['data'] = $data;
                } else {
                    $this->resp['warn'] = '上传失败';
                }
            }
        }
    }

    /**
     * 删除附件
     */
    public function delFile()
    {
        $power = $this->post('power');
        $file = query("file", " id = '" . $this->post('id') . "' ");
        if (!$this->power($power, 'edit')) {
            $this->resp['warn'] = '你还没有编辑的权限';
        } elseif (empty($file['id'])) {
            $this->resp['warn'] = '没有找到此文件';
        } else {
            $this->resp['warn'] = delete(array('table' => 'file', 'where' => " id = '$file[id]'"));
            if ($this->resp['warn'] == '删除成功') {
                fileDelete($file['src']);
                $this->code = 1;
            }
        }
    }
}

